package com.showde.shoppingmall.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.kim.androidbase.db.DBCommandInteface;
import com.kim.androidbase.db.DBSession;
import com.kim.androidbase.db.IDBUtils.QueryCompleteCallback;
import com.kim.androidbase.db.SQLiteCriteria;
import com.showde.shoppingmall.db.constant.AddressConstant;
import com.showde.shoppingmall.domain.Address;

public class AddressDBCommand implements DBCommandInteface<Address, String> {

	private static final String TAG = "AddressDBCommand";

	DBSession dbSession;

	public AddressDBCommand(Context context) {
		dbSession = new DBSession(context, new DBHelper(context));
	}

	@Override
	public void add(Address address) {
		dbSession.insert(AddressConstant.TABLENAME, getValues(address),
				SQLiteDatabase.CONFLICT_REPLACE);
	}

	@Override
	public void add(List<Address> addresses) {
		List<ContentValues> contentValuesList = new ArrayList<ContentValues>();
		for (Address address : addresses) {
			contentValuesList.add(getValues(address));
		}
		dbSession.insertBatch(AddressConstant.TABLENAME, contentValuesList,
				SQLiteDatabase.CONFLICT_REPLACE);
	}

	@Override
	public void delete(String pk) {
		dbSession.delete(AddressConstant.TABLENAME, AddressConstant.RECIPIENTID
				+ "=?", new String[] { pk });
	}

	@Override
	public void delete(SQLiteCriteria sqLiteCriteria) {

	}

	@Override
	public void modify(SQLiteCriteria sqLiteCriteria) {
		// TODO Auto-generated method stub

	}

	@Override
	public void modify(String pk, SQLiteCriteria sqLiteCriteria) {
		// TODO Auto-generated method stub

	}

	@Override
	public Address query(String pk) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Address> query(SQLiteCriteria sqLiteCriteria) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Address> queryAll() {
		final List<Address> addresses = new ArrayList<Address>();
		dbSession.query("select * from " + AddressConstant.TABLENAME, null,
				new QueryCompleteCallback() {

					@Override
					public void onQueryError(String errorMsg) {
						Log.d(TAG, "queryAll:" + errorMsg);
					}

					@Override
					public void onQueryComplete(Cursor cursor) {
						if (cursor == null || cursor.getCount() == 0) {
							return;
						}

						while (cursor.moveToNext()) {
							addresses.add(getAddress(cursor));
						}
					}
				});
		return addresses;
	}

	private Address getAddress(Cursor cursor) {
		Address address = new Address();

		address.setRecipientId(dbSession.getString(cursor,
				AddressConstant.RECIPIENTID));
		address.setRecipient(dbSession.getString(cursor,
				AddressConstant.RECIPIENT));
		address.setAddressContent(dbSession.getString(cursor,
				AddressConstant.ADDRESSCONTENT));
		address.setPhoneNum(dbSession.getString(cursor,
				AddressConstant.PHONENUM));
		address.setHomeNum(dbSession.getString(cursor, AddressConstant.HOMENUM));
		address.setPostcode(dbSession.getString(cursor,
				AddressConstant.POSTCODE));

		return address;
	}

	private ContentValues getValues(Address address) {
		ContentValues contentValues = new ContentValues();

		contentValues
				.put(AddressConstant.RECIPIENTID, address.getRecipientId()); // 地址唯一标识
		contentValues.put(AddressConstant.RECIPIENT, address.getRecipient()); // 收件人
		contentValues.put(AddressConstant.ADDRESSCONTENT,
				address.getAddressContent()); // 地址内容
		contentValues.put(AddressConstant.PHONENUM, address.getPhoneNum()); // 电话号码
		contentValues.put(AddressConstant.HOMENUM, address.getHomeNum()); // 家庭电话
		contentValues.put(AddressConstant.POSTCODE, address.getPostcode()); // 邮编

		contentValues.put(AddressConstant.PROVINCE, "");
		contentValues.put(AddressConstant.CITY, "");
		contentValues.put(AddressConstant.STREET, "");

		return contentValues;
	}

}
